var myOrder = angular.module("myOrderApp",["scService"]);
myOrder.controller("myOrderController",function($scope,$http,scCodeService,$location,$stateParams,commonService,jsonToStr){
	// 调用根据用户id查询订单列表的接口 如无订单，则提示用户。如存在订单，则显示订单列表。
	/*$scope.getOrderList = function(){
		var postData ={
			"userId":"01042016090600010021",
			"pageNo":"1"
		};
		commonService.commonAjaxByPost("fund/getFundList",jsonToStr.transform(postData))
		.then(function(data){

		});
	};
	$scope.getOrderList();*/
	//进行新数据组装到老数据上
	$scope.dataToOrderList=function(obj) {
		$scope.chargelen=$scope.orderList.length;
		$scope.appserver='http://' + window.location.host + '/appserver/static/wechat-ht/index.html?#';
		angular.forEach(obj, function(data,index,array){
			if(array[index].status<2){
				$scope.status="未支付";
				$scope.link=$scope.appserver+'/orderPay/'+array[index].orderNo;
			}else if(array[index].status>=2){
				$scope.status="已完成";
				$scope.link=$scope.appserver+'/orderDetail/'+array[index].orderNo;
			}
			$scope.orderList[$scope.chargelen]={
					"orderNo":array[index].orderNo,
					"createTime":array[index].createTime,
					"caculatedTotalPrice":array[index].caculatedTotalPrice,
					"status":$scope.status,
					"link":$scope.link
				};
			$scope.chargelen++;
			
		});
	}
	//定义获取订单参数
	$scope.orderList=[];
	$scope.pageNo=1; //页码
	$scope.isPush=true; //是否需要加载数据
	$scope.hasOrder=false; //是否存在订单
	//根据用户id获取订单列表
	$scope.getOrderList=function () {
		var userId = localStorage.getItem("userId");
		/*$http({
				url:'http://localhost:8080/appserver/getCurrentChargerInfo',
				method:'get',
				params:{'qr_code':"000000000022",'plug_id':"1","source":"124","operator":"0201610047"},
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
				},
			})
			.success(function(data){
				if(data.data){
					
				}
			})*/
		var postData = {
			'userId':userId, //userId
			'pageNo':$scope.pageNo
		}
		scCodeService.getOrderList(postData).then(function(data){
				
				if(data.code == "200"){
					 if(data.data!="" && data.data.length>0 && data.data!=null){
						
						//将新数据组装到之前数据上进行显示
						$scope.dataToOrderList(data.data);
						//$scope.orderList=data.data;
						if(data.data.length<20){
							$scope.isPush=false;
						}
					}else{
						$scope.isPush=false;
					}
					if($scope.pageNo==1 && data.data=="" || data.data.length==0 || data.data==null){
							$scope.hasOrder=true;
					}
				}
			})
	}
	$scope.getOrderList();
	//上拉加载数据
	window.onscroll=function() {
		
		var wh=window.pageYOffset+window.screen.height;
		var dh=document.body.scrollHeight;
		
		if(wh>=dh && $scope.isPush){
			
			$scope.pageNo++;
			$scope.getOrderList();
		}
	}


});

 //点击某一条订单

/*myOrder.directive('getStatus',function($location){
	return {
		restrict: 'A',
		link: function($scope, iElm, iAttrs) {
			iElm.on("click",function(){
				
				var status = iAttrs.status;
				switch(status){
					case "未支付":
					$location.path("/orderPay/001");
					break;

					case "充电中":
					$location.path("/charging");
					break;

					case "已完成":
					$location.path("/orderDetail/001");
					break;
				}
			})
		}
	};
});*/